import {defineStore} from 'pinia'
import {reactive} from "vue";
import {selectDbSource, selectDbType, selectEnv, selectSyncMode} from "@/api/api.dictionary";

export const DictionaryStore = defineStore('dic', () => {
    const dictionary = reactive({
        env: [],
        syncMode: [],
        dbSource: [],
        dbType: []
    });

    const lodeEnv = async () => {
        // 如果数据存在，则不重新加载数据
        if (dictionary.env && dictionary.env.length !== 0) {
            return;
        }
        await selectEnv({}).then(res => {
            dictionary.env = res.data || [];
        })
    }

    const lodeSyncMode = () => {
        // 如果数据存在，则不重新加载数据
        if (dictionary.syncMode && dictionary.syncMode.length !== 0) {
            return;
        }
        selectSyncMode({}).then(res => {
            dictionary.syncMode = res.data || [];
        })
    }

    const lodeDbSource = () => {
        // 如果数据存在，则不重新加载数据
        if (dictionary.dbSource && dictionary.dbSource.length !== 0) {
            return;
        }
        selectDbSource({}).then(res => {
            dictionary.dbSource = res.data || [];
        })
    }

    const lodeDbType = () => {
        // 如果数据存在，则不重新加载数据
        if (dictionary.dbType && dictionary.dbType.length !== 0) {
            return;
        }
        selectDbType({}).then(res => {
            dictionary.dbType = res.data || [];
        })
    }

    return {
        dictionary,
        lodeEnv,
        lodeSyncMode,
        lodeDbSource,
        lodeDbType
    }
})
